package com.qf.factory;

import java.io.IOException;
import java.util.Properties;

/**
 * 工厂: 生产对象
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class BeanFactory {

    //属性对象
    private static Properties properties = new Properties();
    //静态代码块：加载该类型 执行静态代码块
    static {
        try {
            properties.load(BeanFactory.class.getResourceAsStream("/beans.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据名称获取对象
     * @param name
     * @return
     */
    public static Object getBean(String name){
        String value = (String) properties.get(name);

        try {
            //通过反射创建对象并返回
            Class clazz = Class.forName(value);

            Object obj = clazz.newInstance();
            return obj;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return value;
    }

}
